//
//  GameLayer.m
//  lust
//
//  Created by fan on 13-3-28.
//
//

#import "GameLayer.h"
#import "SysMenu.h"
@implementation GameLayer
+(CCScene*)scene
{
    CCScene* scene=[CCScene node];
    [scene addChild:[GameLayer node]];
    return scene;
}
-(id)init
{
    self =[super init];
    if (self) {
        CGSize size=[CCDirector sharedDirector].winSize;
        CCSprite* sp=[CCSprite spriteWithFile:@"space.png"];
        sp.anchorPoint=CGPointMake(0.5, 0.5);
        sp.position=ccp(size.width/2, size.height/2);
//        sp.opacity=100;
        sp.rotation=90;
//        [self addChild:sp z:0 tag:1];
        
        CCTMXTiledMap* gameWorld=[CCTMXTiledMap tiledMapWithTMXFile:@"Level1.tmx"];
        [self addChild:gameWorld z:0 tag:9];
        
        CCSpriteBatchNode* mgr=[CCSpriteBatchNode batchNodeWithFile:@"flight.png" capacity:5];
        [self addChild:mgr z:0 tag:4];
        
        
        flight=[CCSprite spriteWithTexture:mgr.texture rect:CGRectMake(0, 0, 31, 30)];
        flight.scale=1.6;
        flight.position=ccp(size.width/2, 0);
        flight.anchorPoint=CGPointMake(0.5, 0);
        
        
        
            CCSpriteFrame* frame0=[CCSpriteFrame frameWithTexture:mgr.texture rect:CGRectMake(0, 0, 31, 30)];
            CCSpriteFrame* frame1=[CCSpriteFrame frameWithTexture:mgr.texture rect:CGRectMake(32, 0, 31, 30)];
            CCSpriteFrame* frame2=[CCSpriteFrame frameWithTexture:mgr.texture rect:CGRectMake(2*32, 0, 31, 30)];
        
        
        NSArray* arr=[NSArray arrayWithObjects:frame0,frame1,frame2, nil];

        CCAnimation* a=[CCAnimation animationWithSpriteFrames:arr];
        a.delayPerUnit=.2f;
        id action=[CCAnimate actionWithAnimation:a];
        [flight runAction:[CCRepeatForever actionWithAction:action]];
        [mgr addChild:flight z:1 tag:5];
        
        time=0;
        CCLabelTTF *timeLabel = [CCLabelTTF labelWithString:@"time: 0"  fontName:@"Marker Felt"  fontSize:21];
        timeLabel.position=ccp(50,290);
        [self addChild:timeLabel z:2 tag:3];
        [self schedule:@selector(step:) interval:1];
        self.isTouchEnabled=YES;
        
        CCMenuItemFont * systemMenu=[CCMenuItemFont itemWithString:@"Menu" target:self selector:@selector(menu:)];
        CCMenu* menu=[CCMenu menuWithItems:systemMenu, nil];
        menu.anchorPoint=CGPointMake(1, 1);
        
        menu.position=ccp(size.width,size.height);
        [self addChild:menu z:10 tag:10];
//        [self actionMenu];
//        [self intervalActionMenu];
        [self MultiActionMenu];
    }
    return self;
}

-(void)actionMenu
{
    CCMenuItem* place=[CCMenuItemFont itemWithString:@"Place" target:self selector:@selector(onMenuItem:)];
    place.tag=1;
    CCMenuItem* hide=[CCMenuItemFont itemWithString:@"Hide" target:self selector:@selector(onMenuItem:)];
    hide.tag=2;
    CCMenuItem* show=[CCMenuItemFont itemWithString:@"Show" target:self selector:@selector(onMenuItem:)];
    show.tag=3;
    CCMenuItem* toggle=[CCMenuItemFont itemWithString:@"Toggle" target:self selector:@selector(onMenuItem:)];
    toggle.tag=4;
    CCMenu* actionMenu=[CCMenu node];
    [actionMenu addChild:place];
    [actionMenu addChild:hide];
    [actionMenu addChild:show];
    [actionMenu addChild:toggle];
    [actionMenu alignItemsVertically];
    actionMenu.position=ccp(50, 200);
    actionMenu.anchorPoint=CGPointMake(0, 1);
    [self addChild:actionMenu];
}
-(void)intervalActionMenu
{
    NSArray* arr=[NSArray arrayWithObjects:@"MoveTo",@"MoveBy",@"JumpTo",@"JumpBy",@"BezierBy",@"ScaleTo",@"ScaleBy",@"RoateTo",@"RoateBy",@"Blink",@"TintTo",@"TintBy",@"FadeTo",@"FadeIn",@"FadeOut", nil];
    CCMenu* menu=[CCMenu node];
    [CCMenuItemFont setFontSize:15];
    for (int i=0; i<arr.count; i++) {
        CCMenuItem* item=[CCMenuItemFont itemWithString:[arr objectAtIndex:i] target:self selector:@selector(onIntervalAction:)];
        item.tag=i;
        [menu addChild:item];
    }
    [menu alignItemsVertically];
    menu.position=ccp(50, 200);
    menu.anchorPoint=CGPointMake(0, 1);
    [self addChild:menu];
}

-(void)onIntervalAction:(id)sender
{
    CGSize s=[[CCDirector sharedDirector] winSize];
    CCMenuItem* mi=(CCMenuItem*)sender;
    ccBezierConfig bezier;
    switch (mi.tag) {
        case 0:
        {
            [flight runAction:[CCMoveTo actionWithDuration:2 position:ccp(s.width-50, s.height -50)]];
        }
        break;
        case 1:
        {
            [flight runAction:[CCMoveBy actionWithDuration:20
                position:ccp(150,150)]];
        }
        break;
        case 2:
        {
            [flight runAction:[CCJumpTo actionWithDuration:2 position:ccp(150,50) height:30 jumps:5]];
        }
        break;
        case 3:
        {
            [flight runAction:[CCJumpBy actionWithDuration:2 position:ccp(150, 150) height:30 jumps:5]];
        }
        break;
        case 4:
        {
            bezier.controlPoint_1=ccp(0, s.height/2);
            bezier.controlPoint_2=ccp(300,-s.height/2);
            bezier.endPosition=ccp(-100, 100);
            [flight runAction:[CCBezierBy actionWithDuration:3 bezier:bezier]];
        }
        break;
        case 5:
        {
            [flight runAction:[CCScaleTo actionWithDuration:2 scale:4]];
        }
            break;
        case 6:
        {
            [flight runAction:[CCScaleBy actionWithDuration:2 scale:0.5]];
        }
            break;
        case 7:
        {
            [flight runAction:[CCRotateTo actionWithDuration:2 angle:180]];
        }break;
        case 8:
        {
            [flight runAction:[CCRotateBy actionWithDuration:2 angle:-180]];
        }break;
        case 9:
        {
            [flight runAction:[CCBlink actionWithDuration:3 blinks:5]];
        }break;
        case 10:{
            [flight runAction:[CCTintTo actionWithDuration:2 red:255 green:0 blue:0]];
        }break;
        case 11:{
            [flight runAction:[CCTintBy actionWithDuration:0.5 red:0 green:255 blue:255]];
        }break;
        case 12:
        {
            [flight runAction:[CCFadeTo actionWithDuration:2 opacity:80]];
        }break;
        case 13:
        {
            [flight runAction:[CCFadeIn actionWithDuration:1.0f]];
        }break;
        case 14:
        {
            [flight runAction:[CCFadeOut actionWithDuration:1.0f]];
        }break;

    }
}
-(void)MultiActionMenu
{
     NSArray* arr=[NSArray arrayWithObjects:@"Sequence",@"Spawn",@"Repeat",@"Revers",@"Animation",@"RepeatForever", nil];
    CCMenu* menu=[CCMenu node];
    for (int i=0; i<arr.count; i++) {
        CCMenuItem* item=[CCMenuItemFont itemWithString:[arr objectAtIndex:i] target:self selector:@selector(onMultiActionItem:)];
        item.tag=i;
        [menu addChild:item];
    }
    [menu alignItemsVertically];
    menu.anchorPoint=ccp(0, 1);
    menu.position=CGPointMake(50, 200);
    [self addChild:menu];            
}
    
-(void)onMultiActionItem:(id)sender
{
    CGSize size=[CCDirector sharedDirector].winSize;
    CCMenuItem* mi=(CCMenuItem*)sender;
    switch (mi.tag) {
        case 0:
        {
            id ac0=[CCPlace actionWithPosition:CGPointMake(size.width/2, 50)];
            id ac1=[CCMoveTo actionWithDuration:2 position:ccp(size.width-50, size.height-50)];
            id ac2=[CCJumpTo actionWithDuration:2 position: ccp(150, 50) height:30 jumps:5];
            id ac3=[CCBlink actionWithDuration:2 blinks:3];
            id ac4=[CCTintBy actionWithDuration:0.5 red:0 green:255 blue:255];
            
            [flight runAction:[CCSequence actions:ac0,ac1,ac2,ac3,ac4,ac0, nil]];
        }
            break;
            
        case 1:
        {
            CGSize s=[CCDirector sharedDirector].winSize;
            CGPoint p=ccp(s.width/2, 50);
            flight.rotation=0;
            [flight setPosition:p];
            
            id ac0=[CCMoveTo actionWithDuration:1 position:ccp(s.width-50, s.height-50)];
            id ac1=[CCRotateTo actionWithDuration:2 angle:180];
            id ac2=[CCScaleTo actionWithDuration:1 scale: 4];
            id ac3=[CCScaleBy actionWithDuration:1 scale:0.5];
            id seq=[CCSequence actions:ac2,ac3, nil];
            [flight runAction:[CCSpawn actions:ac0,ac1,seq,nil]];
        }
            break;
        case 2:{
            CGSize s=[[CCDirector sharedDirector] winSize];
            CGPoint p=ccp(s.width/2, 50);
            flight.rotation=0;
            [flight setPosition:p];
            id ac0=[CCMoveTo actionWithDuration:2 position:ccp(s.width-50, s.height-50)];
            id ac1=[CCJumpBy actionWithDuration:2 position:ccp(-400, -200) height:30 jumps:5];
            id ac2=[CCJumpBy actionWithDuration:2 position:ccp(s.width/2, 0) height:20 jumps:3];
            id seq=[CCSequence actions:ac0,ac1,ac2,nil];
            [flight runAction:[CCRepeat actionWithAction:seq times:3]];
        }break;
        case 3:
        {
            CGSize s=[[CCDirector sharedDirector] winSize];
            CGPoint p=ccp(s.width/2, 50);
            flight.rotation=0;
            [flight setPosition:p];
            
            id ac0=[CCMoveBy actionWithDuration:2 position:ccp(190, 220)];
            id ac1=[ac0 reverse];
            [flight runAction:[CCRepeat actionWithAction:[CCSequence actions:ac0,ac1,nil] times:2]];
        }break;
        case 4:break;
        case 5:
        {
            CGSize s=[[CCDirector sharedDirector] winSize];
            CGPoint p=ccp(s.width/2, 50);
            ccBezierConfig bezier;
            flight.rotation = 0;
            [flight setPosition:p];
//            bezier. = ccp(0,0);
            bezier.controlPoint_1 = ccp(0, s.height/2); bezier.controlPoint_2 = ccp(300, -s.height/2); bezier.endPosition = ccp(300,100);
            id ac10 = [CCBezierBy actionWithDuration:3 bezier:bezier];
            id ac11 = [CCTintBy actionWithDuration:0.5 red:0 green:255 blue:255];
            id ac1 = [CCSpawn actions:ac10, [CCRepeat actionWithAction:ac11 times:4], nil];
            id ac2 = [CCSpawn actions:[ac10 reverse], [CCRepeat actionWithAction:ac11 times:4], nil];
            // 第二个无限期连续运劢。
            [flight runAction:[CCRepeatForever actionWithAction:[CCSequence actions:ac1, ac2,nil]]];
        }break;
    }
}
                          
-(void)onMenuItem:(id)sender
{
    CCMenuItem* mi=(CCMenuItem*)sender;
    switch (mi.tag) {
        case 1:{
            CGSize size=[CCDirector sharedDirector].winSize;
            [flight runAction:[CCPlace actionWithPosition:ccp(size.width/2,size.height/2)]];
        }
            break;
        case 2:
        {
            [flight runAction:[CCHide action]];
        }
            break;
        case 3:{
            [flight runAction:[CCShow action]];}
            break;
        case 4:{
            [flight runAction:[CCToggleVisibility action]];}
            break;
           }
}
-(void)menu:(id)sender
{
    CCScene* s=[CCScene node];
    [s addChild:[SysMenu node]];
    [[CCDirector sharedDirector] replaceScene:s];
}

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location=[touch locationInView:[touch view]];
    CGPoint convertedLocation=[[CCDirector sharedDirector]convertToGL:location];
    
    [flight runAction:[CCMoveTo actionWithDuration:1 position:convertedLocation]];
}

-(void) registerWithTouchDispatcher
{
//    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
    [[[CCDirector sharedDirector]touchDispatcher]addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    return YES;
}

//-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//    UITouch* touch=[touches anyObject];
//    CGPoint location=[touch locationInView:[touch view]];
//    CGPoint convertedLocation=[[CCDirector sharedDirector]convertToGL:location];
//    
//    [flight runAction:[CCMoveTo actionWithDuration:1 position:convertedLocation]];
//}
-(void)step:(ccTime)dt
{
    time+=dt;
    CCLabelTTF* tl=(CCLabelTTF*)[self getChildByTag:3];
    [tl setString:[NSString stringWithFormat:@"%@%d",@"time: ",(int)time]];
}
@end
